{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Part  1 - Introduction to Grid\n",
    "\n",
    "##### Grid is a platform to **train**, **share** and **manage** models and datasets in a **distributed**, **collaborative** and **secure way**.  \n",
    "\n",
    "&nbsp;    Grid platform aims to be a secure peer to peer platform. It was created to use pysyft's features to perform federated learning processes without the need to manage distributed workers directly. Nowadays, to perform machine learning process with PySyft library, the user needs to manage directly all the workers' stuff (start nodes, manage node connections, turn off nodes, etc). Grid platform solves this in a transparent way. The user won't need to know about how the nodes are connected or where is some specific dataset.  \n",
    "\n",
    "Authors:\n",
    " - Ionésio Junior - Github: [IonesioJunior](https://github.com/IonesioJunior)\n",
    "\n",
    "## Why should we use grid?\n",
    "As mentioned before, the grid is basically a platform that uses PySyft library to manage distributed workers providing some special features.  \n",
    "\n",
    "**We should use grid to:**\n",
    "- Train models using datasets that we've never seen (without getting access to its real values).\n",
    "- Train a model with encrypted datasets.\n",
    "- Provide Secure MLaaS running encrypted model inferences across grid network.\n",
    "- We can serve an encrypted model without giving its real weights to anyone.\n",
    "- We can run encrypted inferences without sending our private data to anyone.\n",
    "- Mitigate risks and impacts using Federated Learning's **\"privacy by design\"** property.\n",
    "- Manage the privacy level of datasets stored at grid network allowing/disallowing access to them.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## How it works?\n",
    "We have two concepts of grid: **Private Grid Platform** and **Public Grid Platform**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Private Grid\n",
    "###### Private Grid is used to build private's grid platform.\n",
    "&nbsp; It will empower you with the control to manage the entire platform, you'll be able to create, remove and manage all nodes connected on your grid network. However, with power and control, you'll need to take care of the grid platform by yourself. \n",
    "\n",
    "- To build it, you'll need to know previously where is each grid node that you want to use in your infrastructure.\n",
    "- You will need to configure scale up/scale down routines (nº of nodes) by yourself.\n",
    "- You can add pr remove nodes.\n",
    "- You will be connected directly with these nodes.\n",
    "\n",
    "<p align=\"center\">\n",
    "<img height=\"600px\" width=\"600px\" src=\"https://github.com/OpenMined/rfcs/blob/master/20190821-grid-platform/DHT-grid.png?raw=true\">\n",
    "</p>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import syft as sy\n",
    "import torch as th\n",
    "from syft.workers.node_client import NodeClient\n",
    "\n",
    "hook = sy.TorchHook(th)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# How to build / use a private grid network\n",
    "\n",
    "# 1 - Start the grid nodes.\n",
    "# 2 - Connect to them directly\n",
    "# 3 - Create Private Grid using their instances.\n",
    "\n",
    "# We need to know the address of every node.\n",
    "node1 = NodeClient(hook, \"ws://localhost:3000\")\n",
    "node2 = NodeClient(hook, \"ws://localhost:3001\")\n",
    "node3 = NodeClient(hook, \"ws://localhost:3002\")\n",
    "node4 = NodeClient(hook, \"ws://localhost:3003\")\n",
    "\n",
    "my_grid = sy.PrivateGridNetwork(node1,node2,node3,node4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Public Grid\n",
    "###### Public Grid offers the oportunity to work as a real collaborative platform.  \n",
    "&nbsp;Unlike the private grid, anyone has the power to control all nodes connected to the public grid, the platform will be managed by grid gateway. This component will update the network automatically and perform queries through the nodes. It's important to note that the grid gateway can **only perform non-privileged commands** on grid nodes, it will avoid some vulnerabilities. \n",
    "\n",
    "Therefore, anyone can register a new node, upload new datasets using their nodes to share it with everyone in a secure way.\n",
    "\n",
    "\n",
    "Public Grid should work as a **Secure Data Science platform** (such as Kaggle, but using Privacy-Preserving concepts):\n",
    "    - We send pointers to datasets instead of real datasets.\n",
    "    - We can share our models across the network in an encrypted way.\n",
    "    - We can run inferences using our sensitive datasets without send the real value of it to anyone.\n",
    "<p align=\"center\">\n",
    "<img height=\"600px\" width=\"600px\" src=\"https://github.com/OpenMined/rfcs/blob/master/20190821-grid-platform/partially_grid.png?raw=true\">\n",
    "</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# How to build/use a public grid network\n",
    "\n",
    "# 1 - Start the grid nodes\n",
    "# 2 - Register them at grid gateway component\n",
    "# 3 - Use grid gateway to perform queries.\n",
    "\n",
    "# You just need to know the address of grid gateway.\n",
    "my_grid = sy.PublicGridNetwork(hook, \"http://localhost:5000\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Congratulations!!! - Time to Join the Community!\n",
    "\n",
    "Congratulations on completing this notebook tutorial! If you enjoyed this and would like to join the movement toward privacy preserving, decentralized ownership of AI and the AI supply chain (data), you can do so in the following ways!\n",
    "\n",
    "### Star PySyft on GitHub\n",
    "\n",
    "The easiest way to help our community is just by starring the GitHub repos! This helps raise awareness of the cool tools we're building.\n",
    "\n",
    "- [Star PySyft](https://github.com/OpenMined/PySyft)\n",
    "\n",
    "### Join our Slack!\n",
    "\n",
    "The best way to keep up to date on the latest advancements is to join our community! You can do so by filling out the form at [http://slack.openmined.org](http://slack.openmined.org)\n",
    "\n",
    "### Join a Code Project!\n",
    "\n",
    "The best way to contribute to our community is to become a code contributor! At any time you can go to PySyft GitHub Issues page and filter for \"Projects\". This will show you all the top level Tickets giving an overview of what projects you can join! If you don't want to join a project, but you would like to do a bit of coding, you can also look for more \"one off\" mini-projects by searching for GitHub issues marked \"good first issue\".\n",
    "\n",
    "- [PySyft Projects](https://github.com/OpenMined/PySyft/issues?q=is%3Aopen+is%3Aissue+label%3AProject)\n",
    "- [Good First Issue Tickets](https://github.com/OpenMined/PySyft/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22)\n",
    "\n",
    "### Donate\n",
    "\n",
    "If you don't have time to contribute to our codebase, but would still like to lend support, you can also become a Backer on our Open Collective. All donations go toward our web hosting and other community expenses such as hackathons and meetups!\n",
    "\n",
    "[OpenMined's Open Collective Page](https://opencollective.com/openmined)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
